/**
 * Created by luckyjing on 16/2/4.
 */
var express = require('express'),
    config = require('./config'),
    morgan = require('morgan'),
    compress = require('compression'),
    bodyParser = require('body-parser'),
    methodOverride = require('method-override'),
    session = require('express-session'),
    passport = require('passport');
module.exports = function () {
    var app = express();
    // 开发环境和发布环境
    if (process.env.NODE_ENV === 'development') {
        app.use(morgan('dev'));
    } else if (process.env.NODE_ENV === 'production') {
        app.use(compress());
    }
    // parsing req.body
    app.use(bodyParser.urlencoded({
        extended: true
    }));
    app.use(bodyParser.json());
    // end of parsing req.body

    app.use(methodOverride());

    //
    app.use(session({
        saveUninitialized: true,
        resave: true,
        secret: config.sessionSecret
    }));

    //
    app.use(passport.initialize());
    app.use(passport.session());

    require('../app/routes/index.server.routes')(app);
    require('../app/routes/users.server.routes')(app);
    require('../app/routes/tool.server.routes')(app);
    require('../app/routes/rent.server.routes')(app);

    // 静态文件
    app.use(express.static('./public'));
    app.use(express.static('./app/views'));
    return app;
};